cc.Class({
    extends: cc.Component,

    ctor(){
        this.nodes = [];
        this.gameStep = [];
        this.isInit = false;
    },
    properties: {
        prefabArea: {
            type: cc.Prefab,
            default: null
        },
        game: {
            type: cc.Node,
            default: null 
        }
    },
    callEnd(index){
        this.game.emit('areaPlayEnd', index);
    },
    playAniOut(index){
        let curStep = this.gameStep[index];
        let curNodes = this.nodes[index];
        for(let i=0; i<curNodes.length; i++){
            let tt = cc.tween(curNodes[i])
            tt.by(curStep.aniTime, {position: cc.v2(750, 0)})
            .call(()=>{
                curNodes[i].setPosition(cc.v2(-750, curNodes[i].position.y))
                if(i==0){
                    this[curStep.callFn] && this[curStep.callFn](index)
                }
            })
            tt.start()
        }
    },
    playAni(obj){

        if(!this.isInit) return false

        let curStep = this.gameStep[obj.index];
        let curNodes = this.nodes[obj.index];

        for(let i=0; i<curNodes.length; i++){
            let richBg = curNodes[i].getChildByName('richbg');
            let rich = richBg.getChildByName('rich');
            let richArea = rich.getComponent(cc.RichText);
            let text = this.gameStep[obj.index].areas[i];
            if(text.indexOf('{{') != -1){
                richArea.string = text.replace(/{{(\w+)}}/g, (str, $1)=>{
                    return obj[$1]
                })
            }
        }

        for(let i=0; i<curNodes.length; i++){
            let tt = cc.tween(curNodes[i])
            tt.delay(curStep.delay*i).by(curStep.aniTime, {position: cc.v2(750, 0)})
            if(i == curNodes.length - 1){
                tt.delay(curStep.delay).call(()=>{
                    if(this[curStep.callFn]){
                        this.playAniOut(obj.index)
                    }
                })
            }
            tt.start()
        }
    },
    initPrefab(){
        let arr = this.gameStep;
        for(let i=0; i<arr.length; i++){
            let aNode = [];
            let prevH = 0;
            for(let j=0; j<arr[i].areas.length; j++){
                let cur = cc.instantiate(this.prefabArea);
                
                let richBg = cur.getChildByName('richbg');
                let rich = richBg.getChildByName('rich');
                let richArea = rich.getComponent(cc.RichText)
                richArea.string = arr[i].areas[j];
                cur.setPosition(cc.v2(-750, arr[i].startY - prevH - arr[i].step*j))
                this.node.addChild(cur)
                cur.height = richBg.height = Math.max(rich.height, arr[i].minH);
                prevH+=cur.height;
                aNode.push(cur);
            }
            this.nodes.push(aNode)
        }
    },
    init(stepArr){
        console.log('before init', stepArr)
        this.gameStep = stepArr;
        this.isInit = true;
        this.initPrefab();
    },
    onLoad () {
        console.log('beofre onload')
        this.node.on('play', this.playAni.bind(this))
        this.node.on('init', this.init.bind(this))
    },
    start () {

    },
    // update (dt) {},
});
